package com.web.controller;

import com.web.bean.教师情况;
import com.web.dao.教师情况Dao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

//定义当前请求处理的访问uri
@WebServlet("/insertTeacher")
public class TeacherInsertController extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        String 教师编号 = req.getParameter("教师编号");
        String 姓名 = req.getParameter("姓名");
        String 性别 = req.getParameter("性别");
        String 职称 = req.getParameter("职称");
        String 院系代号 = req.getParameter("院系代号");

        教师情况 tcr = new 教师情况(教师编号, 姓名, 性别, 职称, 院系代号);
        教师情况Dao dao = new 教师情况Dao();
        boolean isok = false;
        try {
            isok = dao.insertTcr(tcr);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        if (isok) {
            req.getRequestDispatcher("/getAllTeacher").forward(req, resp);
        } else {

            req.setAttribute("message", "添加操作失败，请重新操作");
            req.setAttribute("tcr", tcr);//保存对象数据，在页面表单中回显
            req.getRequestDispatcher("/TeacherInsert.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}